package com.su02.multi.examples.mlp;

import com.su02.multi.chainrule.Constant;
import com.su02.multi.chainrule.Function;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Neuron4MLP extends Function {
    private Linear4MLP linear;
    private ReLU4MLP activation;

    private Neuron4MLP() {}

    @Override
    public Function doBackward(int id) {
        return activation.backward(id);
    }

    @Override
    public double forward(Map<Integer, Double> vector) {
        return activation.forward(vector);
    }

    public static Neuron4MLP fromVector(List<Double> x, Iterator<Integer> idGenerator) {
        return fromFunctions(x.stream().map(Constant::new).toList(), idGenerator);
    }

    public static Neuron4MLP fromFunctions(List<? extends Function> fs, Iterator<Integer> idGenerator) {
        Neuron4MLP neuron = new Neuron4MLP();
        neuron.linear = Linear4MLP.fromFunctions(fs, idGenerator);
        neuron.activation = new ReLU4MLP(neuron.linear);
        neuron.setBs(neuron.activation.getBs());
        return neuron;
    }
}